/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menus;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.MediaComponent;
import com.sun.lwuit.Calendar;
import java.util.Random;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import util.Menu;

/**
 *
 * @author nialldeasy
 */
public class Camera extends Menu {

    private MediaComponent mc = null;
    private Player player = null;
    private VideoControl vc = null;
    int s = 0;
    protected void execute(Form f) {
        f.setBgImage(null);
        try {
            player = Manager.createPlayer("capture://video");
            player.realize();
            mc = new MediaComponent(player);
            mc.start();
          //  f.addComponent(mc);
            vc = (VideoControl) player.getControl("VideoControl");
            vc.setDisplayFullScreen(true);

        } catch (Exception e) {
        }
        try {
            if (player != null) {
                player.start();
            }

        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        this.setLayout(4);

    }

   byte[] snapeshot() throws MediaException {
        VideoControl vidc = (VideoControl) player.getControl("VideoControl");
        byte[] imgData = vidc.getSnapshot(null);

        return imgData;
    }

    public byte[] cheese() {

        byte[] raw = null;
        if(s==0){
        try {
            raw = vc.getSnapshot(null);
            Image image = Image.createImage(raw, 0, raw.length);
                //Label l = new Label(image);
                f.setBgImage(image);
                //f.addComponent(l);
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        try {
            player.stop();
            mc.stop();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        s=1;
        }else{
            try {
                player.start();
                player.realize();
                mc.start();
                s = 0;
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
           // player.realise();
           // player.start();
            
        }

        return raw;
    }

 public boolean writeFile(byte[] data)
    {


     String galleryPath = System.getProperty("fileconn.dir.photos");
     Random gen = new Random();
     String path = galleryPath + "travelCam"+gen.nextInt(500)+gen.nextInt(100)+".jpg";
     
     javax.microedition.io.Connection c = null;
        java.io.OutputStream os = null;
        try {
            c = javax.microedition.io.Connector.open(path, javax.microedition.io.Connector.READ_WRITE);
            javax.microedition.io.file.FileConnection fc =
                    (javax.microedition.io.file.FileConnection) c;
            if (!fc.exists()){
                fc.create();
                System.out.println("im still ");
            }else
                fc.truncate(0);
            os = fc.openOutputStream();
            os.write(data);
            os.flush();
            System.out.println("im still ");
            return true;
        } catch (Exception e) {
            System.out.println("fail");
            return false;
        } finally {
            try {
                if (os != null)
                    os.close();
                if (c != null)
                    c.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void recycleBin() {
        try {
            if (player != null) {
                player.stop();
            }
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        vc = null;
        player = null;
        mc = null;
    }

    public void Key_CENTRE(Form f) {
        
            writeFile(cheese());
    }

    public void update() {

        // f.setBgImage(null);

        // System.out.println("updating Camera");
        f.repaint();
    }
}
